package cn.tworice.client.web;

import cn.tworice.client.service.TopicService;
import cn.tworice.common.entity.Answer;
import cn.tworice.common.entity.Topic;
import cn.tworice.common.entity.dto.TopicDto;
import com.alibaba.fastjson.JSONArray;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 题目管理
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/25 13:37
 */
@RestController
@RequestMapping("/client/topic")
@CrossOrigin
public class TopicClient {
    @Autowired
    private TopicService topicService;

    @GetMapping("")
    public RequestResult list(Integer page, Integer pageSize) {
        RequestResult result = new RequestResult();
        List<TopicDto> list = topicService.getList(page, pageSize);
        Integer total = topicService.getTotal();
        result.appendData("list", list);
        result.appendData("total", total);
        return result;
    }

    @PutMapping("")
    public RequestResult set(Topic topic,String answers) {
        RequestResult result = new RequestResult();
        String id=topicService.setTopic(topic);
        if(topic.getTopicType()==1 && answers!=null) {
            List<Answer> answerList = JSONArray.parseArray(answers, Answer.class);
            topicService.setAnswer(answerList,id);
        }
        return result;
    }

    @DeleteMapping("")
    public RequestResult del(String[] id) {
        RequestResult result = new RequestResult();
        Integer i = topicService.del(id);
        result.setCode(new StatusCode(200,"删除"+i+"条信息"));
        return result;
    }


    /**
     * 通过题目ID获取答案列表
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/25 13:39
     */
    @GetMapping("answer")
    public RequestResult answer(String topicId) {
        RequestResult result = new RequestResult();
        List<Answer> list=topicService.getAnswerByTopicId(topicId);
        result.appendData("list", list);
        return result;
    }

    /**
     * 通过题库ID获取所有题目
     *
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/25 13:40
     */
    @GetMapping("bank")
    public RequestResult bank(String bankId) {
        RequestResult result = new RequestResult();
        List<TopicDto> list=topicService.getTopicListByBankId(bankId);
        result.appendData("list",list);
        return result;
    }
}
